/*
============================================================================
FileName	:	XQChatRosterManager.cpp
Author		:	liyake
Website		:	http://www.liyake.com
Create Data	:	2012-03-15 10:43
Change Data	:	2012-03-15 10:43
Detail		:	roster manager.
============================================================================
*/

#include "XQChatRosterManager.h"

using namespace XQChat;

CXQChatRosterManager* XQChat::CXQChatRosterManager::m_pRosterManager = 0;

CXQChatRosterManager* XQChat::CXQChatRosterManager::getRosterManager()
{
	if(!m_pRosterManager)
		m_pRosterManager = new CXQChatRosterManager();

	return m_pRosterManager;
}

XQChat::CXQChatRosterManager::CXQChatRosterManager( QObject* parent /*= 0*/ )
	: QObject(parent)
	, m_qsHeadersPath(qApp->applicationDirPath()+"/data/header/")
{
	QDir().mkpath(m_qsHeadersPath);
}

XQChat::CXQChatRosterManager::~CXQChatRosterManager( void )
{
	ClearRosters();
}

void XQChat::CXQChatRosterManager::ClearRosters()
{
	QMap<QString,CXQChatRoster*>::iterator iter = m_mapRosters.begin();
	while(iter!=m_mapRosters.end())
	{
		delete iter.value();
		++iter;
	}

	m_mapRosters.clear();
}

CXQChatRoster* XQChat::CXQChatRosterManager::getRosterByFullJid( const QString& jid )
{
	QMap<QString,CXQChatRoster*>::iterator iter = m_mapRosters.find(jid);
	if(iter!=m_mapRosters.end())
		return iter.value();
	
	return 0;
}

bool XQChat::CXQChatRosterManager::addRoster( CXQChatRoster* pRoster )
{
	if(getRosterByFullJid(pRoster->getFullJid()))		//if there already had a same roster, return false.
		return false;

	m_mapRosters[pRoster->getFullJid()]= pRoster;
	return true;
}

QList<CXQChatRoster*> XQChat::CXQChatRosterManager::getRostersByBareJid( const QString& jid )
{
	QList<CXQChatRoster*> listRosters;
    foreach(CXQChatRoster* pRoster, m_mapRosters.values())
	{
		if( pRoster->getBareJid() == jid )
			listRosters.push_back(pRoster);
	}

	return listRosters;
}

QList<CXQChatRoster*> XQChat::CXQChatRosterManager::getRostersAll()
{
	return m_mapRosters.values();
}

